﻿using System;
using System.ComponentModel;

namespace AiFrameDemo.Lib.Model
{
    /// <summary>
    /// Represents a customer.
    /// </summary>
    public class Customer: IDataErrorInfo
    {
        /// <summary>
        /// Gets or sets the ID.
        /// </summary>
        public virtual Guid ID { get; set; }

        /// <summary>
        /// Gets or sets the customer number.
        /// </summary>
        public virtual string CustomerNumber { get; set; }

        /// <summary>
        /// Gets or sets the first name.
        /// </summary>
        public virtual string Firstname { get; set; }

        /// <summary>
        /// Gets or sets the last name.
        /// </summary>
        public virtual string Lastname { get; set; }

        /// <summary>
        /// Gets or sets the birthday.
        /// </summary>
        public virtual DateTime Birthday { get; set; }

        public Customer()
        {
            Birthday = DateTime.Now;
        }

        #region Validation
        
        static readonly string[] ValidatedProperties = 
        { 
            "CustomerNumber",
            "Firstname",
            "Lastname",
            "Birthday"
        };

        public virtual string this[string propertyName]
        {
            get
            {
                if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
                    return null;

                string error = null;

                switch (propertyName)
                {
                    case "CustomerNumber":
                        error = ValidateCustomerNumber();
                        break;
                    case "Firstname":
                        error = ValidateFirstname();
                        break;
                    case "Lastname":
                        error = ValidateLastname();
                        break;
                    case "Birthday":
                        error = ValidateBirthday();
                        break;
                }

                return error;
            }
        }

        private string ValidateCustomerNumber()
        {
            return null;
        }

        private string ValidateBirthday()
        {
            if (Birthday == null || Birthday > DateTime.Now)
                return "Please enter a valid birthday.";
            
            return null;
        }

        private string ValidateLastname()
        {
            if (String.IsNullOrEmpty(Lastname) || Lastname.Trim() == String.Empty)
                return "Please enter a valid first name.";

            return null;
        }

        private string ValidateFirstname()
        {
            if (String.IsNullOrEmpty(Firstname) || Firstname.Trim() == String.Empty)
                return "Please enter a valid last name.";

            return null;
        }

        public virtual string Error
        {
            get { return null; }
        }

        public virtual bool IsValid
        {
            get
            {
                foreach (string property in ValidatedProperties)
                    if (this[property] != null)
                        return false;

                return true;
            }
        }

        #endregion
    }
}
